package com.example.onlinemusic.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.onlinemusic.common.R;
import com.example.onlinemusic.mapper.SongListMapper;
import com.example.onlinemusic.model.domain.SongList;
import com.example.onlinemusic.model.request.SongListRequest;
import com.example.onlinemusic.service.SongListService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SongListServiceImpl extends ServiceImpl<SongListMapper, SongList> implements SongListService {
    @Autowired
    private SongListMapper songListMapper;

    @Override
    public R songList(String style) {
        // 定义查询条件
        QueryWrapper<SongList> queryWrapper = null;
        // 判断查询参数style是否为空
        if (StringUtils.isNoneBlank(style)) {// 不为空的情况下，需要拼接查询条件
            queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("style", style);
        }
        return R.success("查询成功", songListMapper.selectList(queryWrapper));
    }

    @Override
    public R addSongList(SongListRequest songListRequest) {
        SongList songList = new SongList();
        BeanUtils.copyProperties(songListRequest, songList);
        // 设置图片路径
        songList.setPic("https://java-pp.oss-cn-hangzhou.aliyuncs.com/img/songListPic/default.jpg");
        int i = songListMapper.insert(songList);
        if (i > 0) {
            return R.success("添加成功");
        } else {
            return R.error("添加失败");
        }
    }

    @Override
    public R updateSongList(SongListRequest songListRequest) {
        // 判断服务端的数据是否存在
        SongList obj = songListMapper.selectById(songListRequest.getId());
        if (obj == null) {
            return R.error("数据不存在");
        } else {
            BeanUtils.copyProperties(songListRequest, obj);
            // 更新数据
            int i = songListMapper.updateById(obj);
            if (i > 0) {
                return R.success("更新成功");
            } else {
                return R.error("更新失败");
            }
        }
    }

    @Override
    public R deleteSongList(int id) {
        // 判断服务端的数据是否存在
        SongList obj = songListMapper.selectById(id);
        if (obj == null) {
            return R.error("数据不存在");
        } else {
            int i = songListMapper.deleteById(id);
            if (i > 0) {
                return R.success("删除成功");
            } else {
                return R.error("删除失败");
            }
        }
    }
}
